package com.tm.dk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tm.dk.service.MeterService;
import com.tm.entity.MeterEntity;
import com.tm.entity.dto.MeterDto;
import com.tm.exception.utils.Result;
import com.tm.exception.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Random;

/*
* 仪表
*
* */
@RestController
@RequestMapping("meter")
public class MeterController {
    @Autowired
    private MeterService meterService;

    @GetMapping("page")
    public Page<MeterEntity> getlist(@RequestBody MeterDto meterDTO){
        QueryWrapper<MeterEntity> wrapper = new QueryWrapper<MeterEntity>();
        wrapper.like("companyname",meterDTO.getCompanyname());
        Page<MeterEntity> page = new Page<MeterEntity>(meterDTO.getPage(),meterDTO.getPageSize());
        Page<MeterEntity> page1 = meterService.page(page, wrapper);
        return page1;
    }

    @PostMapping("add")
    public Result add(@RequestBody MeterEntity meterEntity){
        boolean save = meterService.save(meterEntity);
        if (save){
            meterEntity.setCreatedtime(new Date());
            return Result.success(ResultCode.SUCCESS);
        }
        return Result.error(ResultCode.ERROR);
    }

    @DeleteMapping
    public Result delete(@RequestBody Integer id){
        boolean removeById = meterService.removeById(id);
        if (removeById){
            return Result.success(ResultCode.SUCCESS);
        }
        return Result.error(ResultCode.ERROR);
    }

    @GetMapping
    public Result getId(@RequestBody MeterEntity meterEntity){
        MeterEntity byId = meterService.getById(meterEntity.getId());
        return Result.success(ResultCode.SUCCESS,byId);
    }




}
